package com.plan.dao.service.card.impl;

import com.google.common.collect.Lists;
import com.plan.base.constant.BaseCode;
import com.plan.base.exception.BizException;
import com.plan.controller.card.request.CompleteAllReq;
import com.plan.dao.mapper.card.CardMapper;
import com.plan.dao.mapper.card.DayPlanMapper;
import com.plan.dao.mapper.card.DayPlanStatMapper;
import com.plan.dao.mapper.card.PointLogMapper;
import com.plan.dao.service.card.DayPlanDaoService;
import com.plan.entity.card.Card;
import com.plan.entity.card.DayPlan;
import com.plan.entity.card.DayPlanStat;
import com.plan.entity.card.PointLog;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class DayPlanDaoServiceImpl implements DayPlanDaoService {
    @Resource
    private DayPlanMapper dayPlanMapper;
    @Resource
    private CardMapper cardMapper;
    @Resource
    private PointLogMapper pointLogMapper;
    @Resource
    private DayPlanStatMapper dayPlanStatMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertDayPlan(DayPlan plan, DayPlanStat dayStat) {
        if (dayStat != null) {
            if (dayStat.getId() == null) {
                dayPlanStatMapper.insert(dayStat);
            } else {
                dayPlanStatMapper.update(dayStat);
            }
        }
        dayPlanMapper.insert(plan);
        return plan.getId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(DayPlan update, Card updateCard) {
        dayPlanMapper.update(update);
        if (updateCard != null) {
            if (1 != cardMapper.update(updateCard)) {
                throw new BizException(BaseCode.DATA_BE_MODIFIED);
            }
        }
    }

    @Override
    public void complete(DayPlan update, Card updateCard, PointLog pointLog) {
        dayPlanMapper.update(update);
        if (updateCard != null) {
            if (1 != cardMapper.update(updateCard)) {
                throw new BizException(BaseCode.DATA_BE_MODIFIED);
            }
        }
        pointLogMapper.batchInsert(Lists.newArrayList(pointLog));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long dayPlanId, DayPlanStat updateDayStat, Long delDayPlanStatId) {
        if (dayPlanId != null) {
            dayPlanMapper.deleteByPk(dayPlanId);
        }
        if (updateDayStat != null) {
            dayPlanStatMapper.update(updateDayStat);
        }
        if (delDayPlanStatId != null) {
            dayPlanMapper.deleteByPk(delDayPlanStatId);
        }
    }

    @Override
    public void copyDayPlanList(Long teamId, Long cardId, List<DayPlan> copyList, List<Integer> dayList) {
        dayPlanMapper.realDelete(teamId, cardId, dayList);
        dayPlanMapper.batchInsert(copyList);
    }

    @Override
    public void copyOneDayPlanList(DayPlanStat stat, List<DayPlan> copyList) {
        dayPlanStatMapper.insert(stat);
        dayPlanMapper.batchInsert(copyList);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void completeForIdList(CompleteAllReq req) {
        int num = dayPlanMapper.completeForIdList(req);
        if (num != req.getIdList().size()) {
            throw new BizException(BaseCode.DATA_BE_MODIFIED);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int realDeleteByCardId(Long teamId, Long cardId) {
        return dayPlanMapper.realDeleteByCardId(teamId, cardId);
    }

}
